//SUMMARY:  This do file performs all analyses for Ballard-Rosa, Carnegie & Schonfeld (2022) "The Geography of Demographic Discontent"

clear all
*set more off

//Set your classpath here:


use "BCS_replication.dta", clear

set scheme s2mono 

//Define variable globals here:
global cluster_level "pais" 
global FE "i.$cluster_level" 
global demogControls "male age  educ7_11 educ12ormore white " 
global countryControls "p_polity2 wdi_gdpcapcur wdi_gdpgr wdi_gdpagr wdi_pop wdi_popden wdi_popurb wdi_taxrev wdi_trade wdi_oilrent"
global crimeControls "crimeProb neighborhoodUnsafe"
global postTreatControls "econBad rightWing trustMil"

//Rescale variables for proper display:
replace tpc_usd = tpc_usd / 100
replace tpt_usd = tpt_usd / 100
replace tct_usd = tct_usd / 100
replace wdi_pop = wdi_pop / 100000
replace wdi_gdpcapcur = wdi_gdpcapcur / 100

//Create labels for display
label var male "Male"
label var educ7_11 "Educ. (7-11 yrs)"
label var educ12ormore "Educ. (12+ yrs)"
label var white "White"
label var econBad "Econ. doing badly"
label var p_polity2 "Democracy"
label var crimeProblem "Crime is problem"
label var neighborhoodUnsafe "Neighborhood unsafe"
label var didVote "Turn out"
label var challenger "Vote for Challenger"

//Generate measure of tax bias
gen taxBias = tpt_usd - tct_usd
label var taxBias "Rural transfer bias"
global taxBiasRange = "-7.5(2.5)5"
global taxBiasTerciles "-3.0 0 .64" //These are medians of each tercile bin, per Hainmueller, Mummolo & Xu

*Figure 1
hist taxBias if pais != 17

*Table 1
probit prefDem i.urban $demogControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/basic_results", replace ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit prefDem i.urban##c.taxBias $demogControls $countryControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/basic_results", append ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
margins, dydx(urban) at(taxBias = ($taxBiasRange)) post 
est store taxBias_democ_fullRange
probit prefDem i.urban##c.taxBias $demogControls $countryControls i.year i.pais, cluster($cluster_level)
margins, dydx(urban) at(taxBias = ($taxBiasTerciles)) post
est store taxBias_democ_terciles

* Figure 2
coefplot (taxBias_democ_fullRange, at yaxis(1) yscale(alt range(-.1 .2) axis(1)) ylabel(-.1(.1).2, axis(1)) recast(line) lwidth(*2) ciopts(recast(rline) lpattern(dash))) (taxBias_democ_terciles, at), addplot(histogram taxBias if pais != 17, frac lcolor(gs13) fcolor(none) yaxis(2) yscale(alt range(0 .4) axis(2)) ylabel(0(.1).4, axis(2)) leg(off)  below) nodrop xlabel($taxBiasRange) yline(0) ///
  ytitle(Marginal effect on Pr(Pref. democ.)) xtitle(Rural transfer bias) title("Urban support for democracy")
graph export "Output\UrbanXTaxBiasME.png", as(png) replace

* Table 2
* Support for coup to address unemp.	
probit coupUnemp i.urban##c.taxBias $demogControls $countryControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/supportCoup", replace ctitle(Coup support (unemp.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
margins, dydx(urban) at(taxBias = ($taxBiasRange)) post
est sto taxBias_coupUnemp_fullRange
probit coupUnemp i.urban##c.taxBias $demogControls $countryControls i.year i.pais, cluster($cluster_level)
margins, dydx(urban) at(taxBias = ($taxBiasTerciles)) post
est sto taxBias_coupUnemp_terciles

* Figure 3
coefplot (taxBias_coupUnemp_fullRange, at yaxis(1) yscale(alt range(-.1 .2) axis(1)) ylabel(-.1(.1).2, axis(1)) recast(line) lwidth(*2) ciopts(recast(rline) lpattern(dash))) (taxBias_coupUnemp_terciles, at), addplot(histogram taxBias if pais != 17, frac lcolor(gs13) fcolor(none) yaxis(2) yscale(alt range(0 .4) axis(2)) ylabel(0(.1).4, axis(2)) leg(off)  below) nodrop xlabel($taxBiasRange) yline(0) ///
  ytitle(Marginal effect on Pr(Support coup)) xtitle(Rural transfer bias) title("Urban support for coup to address unemployment")
graph export "Output\UrbanXTaxBias_coupUnemp.png", as(png) replace

* Support for coup to address corruption
probit coupCorrupt i.urban##c.taxBias $demogControls $countryControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/supportCoup", append ctitle(Coup support (corrupt.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
margins, dydx(urban) at(taxBias = ($taxBiasRange)) post
est sto taxBias_corrupt_fullRange
probit coupCorrupt i.urban##c.taxBias $demogControls $countryControls i.year i.pais, cluster($cluster_level)
margins, dydx(urban) at(taxBias = ($taxBiasTerciles)) post
est sto taxBias_corrupt_terciles

* Figure A1
coefplot (taxBias_corrupt_fullRange, at yaxis(1) yscale(alt range(-.1 .2) axis(1)) ylabel(-.1(.1).2, axis(1)) recast(line) lwidth(*2) ciopts(recast(rline) lpattern(dash))) (taxBias_corrupt_terciles, at), addplot(histogram taxBias if pais != 17, frac lcolor(gs13) fcolor(none) yaxis(2) yscale(alt range(0 .4) axis(2)) ylabel(0(.1).4, axis(2)) leg(off)  below) nodrop xlabel($taxBiasRange) yline(0) ///
  ytitle(Marginal effect on Pr(Support coup)) xtitle(Rural transfer bias) title("Urban support for coup to address corruption")
graph export "Output\UrbanXTaxBias_coupCorrupt.png", as(png) replace

* Support for coup to address crime
probit coupCrime i.urban##c.taxBias $demogControls $countryControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/supportCoup", append ctitle(Coup support (crime)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
margins, dydx(urban) at(taxBias = ($taxBiasRange)) post
est sto taxBias_crime_fullRange
probit coupCrime i.urban##c.taxBias $demogControls $countryControls i.year i.pais, cluster($cluster_level)
margins, dydx(urban) at(taxBias = ($taxBiasTerciles)) post
est sto taxBias_crime_terciles

* Figure A2
coefplot (taxBias_crime_fullRange, at yaxis(1) yscale(alt range(-.1 .2) axis(1)) ylabel(-.1(.1).2, axis(1)) recast(line) lwidth(*2) ciopts(recast(rline) lpattern(dash))) (taxBias_crime_terciles, at), addplot(histogram taxBias if pais != 17, frac lcolor(gs13) fcolor(none) yaxis(2) yscale(alt range(0 .4) axis(2)) ylabel(0(.1).4, axis(2)) leg(off)  below) nodrop xlabel($taxBiasRange) yline(0) ///
  ytitle(Marginal effect on Pr(Support coup)) xtitle(Rural transfer bias) title("Urban support for coup to address crime")
graph export "Output\UrbanXTaxBias_coupCrime.png", as(png) replace

//APPENDIX
* Table A1:  Descriptive stats
eststo clear
estpost tabstat prefDem coupUnemp coupCorrupt coupCrime urban taxBias male age  educ7_11 educ12ormore white p_polity2 wdi_gdpcapcur wdi_gdpgr wdi_gdpagr wdi_pop wdi_popden wdi_popurb wdi_taxrev wdi_trade wdi_oilrent, statistics(count mean sd min max) col(stats) 
esttab using summarystats.tex, cells((count(label(Obs)) mean(label(Mean)fmt(%9.2f)) sd(label(Std Dev)fmt(%9.2f)) min(label(Min)fmt(%9.2f)) max(label(Max)fmt(%9.2f)))) label noobs nonumber nomtitles title(Summary Statistics\label{tab:tab1}) replace 

* Table A2:  Controlling for crime
probit prefDem i.urban##c.taxBias $demogControls $countryControls $crimeControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/controlCrime", replace ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit coupUnemp i.urban##c.taxBias $demogControls $countryControls $crimeControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/controlCrime", append ctitle(Support coup (unemp.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit coupCorrupt i.urban##c.taxBias $demogControls $countryControls $crimeControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/controlCrime", append ctitle(Support coup (corrupt.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit coupCrime i.urban##c.taxBias $demogControls $countryControls $crimeControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/controlCrime", append ctitle(Support coup (crime.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))

* Table A3:  Democ support, additional (potentially post-treatment) controls
probit prefDem i.urban##c.taxBias $demogControls $countryControls econBad i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/addControls", replace ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit prefDem i.urban##c.taxBias $demogControls $countryControls rightWing i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/addControls", append ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit prefDem i.urban##c.taxBias $demogControls $countryControls trustMil i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/addControls", append ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit prefDem i.urban##c.taxBias $demogControls $countryControls $postTreatControls i.year $FE, cluster($cluster_level)
outreg2 using  "Output/addControls", append ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))

* Table A4:  Coups support, additional controls
probit coupUnemp i.urban##c.taxBias $demogControls $countryControls $postTreatControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/coups_addControls", replace ctitle(Coup (unemp.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit coupCorrupt i.urban##c.taxBias $demogControls $countryControls $postTreatControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/coups_addControls", append ctitle(Coup (corrupt.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit coupCrime i.urban##c.taxBias $demogControls $countryControls $postTreatControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/coups_addControls", append ctitle(Coup (crime)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))

* Table A5:  Alternate coding of rural bias
label var tpc_usd "Transfers to producers from consumers (TPC), USD"
probit prefDem i.urban##c.tpc_usd $demogControls  $countryControls $crimeControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/tpc_results", replace ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit coupUnemp i.urban##c.tpc_usd $demogControls $countryControls $crimeControls i.year $FE, cluster($cluster_level)
outreg2 using  "Output/tpc_results", append ctitle(Coup (unemp.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit coupCorrupt i.urban##c.tpc_usd $demogControls $countryControls $crimeControls i.year $FE, cluster($cluster_level)
outreg2 using  "Output/tpc_results", append ctitle(Coup (corrupt.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit coupCrime i.urban##c.tpc_usd $demogControls $countryControls $crimeControls i.year $FE, cluster($cluster_level)
outreg2 using  "Output/tpc_results", append ctitle(Coup (crime)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
* Figure A3
hist tpc_usd

* Table A6:  Multilevel model
//Please note that this will create all the output for this table; however, the presented version in the paper has been reformatted by hand
mixed prefDem urban##c.taxBias $demogControls $countryControls  || pais: || year:, vce(cluster pais)
outreg2 using  "Output/basic_results_MLM", replace ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3) onecol label 
mixed coupUnemp urban##c.taxBias $demogControls $countryControls || pais: || year:, vce(cluster pais)
outreg2 using  "Output/basic_results_MLM", append ctitle(Coup (unemp.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) onecol label 
mixed coupCorrupt urban##c.taxBias $demogControls $countryControls || pais: || year:, vce(cluster pais)
outreg2 using  "Output/basic_results_MLM", append ctitle(Coup (corrupt.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) onecol label 
mixed coupCrime urban##c.taxBias $demogControls $countryControls || pais: || year:, vce(cluster pais)
outreg2 using  "Output/basic_results_MLM", append ctitle(Coup (crime)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) onecol label 

* Table A7:  Democ support w/ bootstrapped SE
probit prefDem i.urban##c.taxBias $demogControls $countryControls i.year i.pais, vce(bootstrap)
outreg2 using  "Output/bootstrapDemoc", replace ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3) onecol label 

* Table A8, Coup support w/ bootstrapped SE
probit coupUnemp i.urban##c.taxBias $demogControls $countryControls i.year i.pais, vce(bootstrap) //cluster($cluster_level)
outreg2 using  "Output/bootstrapCoup", replace ctitle(Coup support (unemp.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) onecol label 
probit coupCorrupt i.urban##c.taxBias $demogControls $countryControls i.year i.pais, vce(bootstrap) //cluster($cluster_level)
outreg2 using  "Output/bootstrapCoup", append ctitle(Coup support (corrupt)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) onecol label 
probit coupCrime i.urban##c.taxBias $demogControls $countryControls i.year i.pais, vce(bootstrap) //cluster($cluster_level)
outreg2 using  "Output/bootstrapCoup", append ctitle(Coup support (crime)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) onecol label 

* Table A9:  Support for authoritarianism
probit coupUnemp i.urban##c.taxBias aut1 $demogControls $countryControls  i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/controlAuth", replace ctitle(Coup (unemp.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3)  label 
probit coupCorrupt i.urban##c.taxBias aut1 $demogControls $countryControls  i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/controlAuth", append ctitle(Coup (corrupt)) drop(i.pais pais* i.year year*) tex(frag) bdec(3)  label 
probit coupCrime i.urban##c.taxBias aut1 $demogControls $countryControls  i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/controlAuth", append ctitle(Coup (crime)) drop(i.pais pais* i.year year*) tex(frag) bdec(3)  label

* Table A10:  Controlling for ideology, turnout, and challenger
probit prefDem i.urban##c.taxBias rightWing $demogControls $countryControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/electionControls", replace ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3)  label 
probit prefDem i.urban##c.taxBias didVote $demogControls $countryControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/electionControls", append ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3)  label
probit prefDem i.urban##c.taxBias challenger $demogControls $countryControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/electionControls", append ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3)  label 

* Table A11:  Using VDem as measure of democracy
probit prefDem i.urban##c.taxBias $demogControls vdem_edcomp_thick wdi_gdpcapcur wdi_gdpgr wdi_gdpagr wdi_pop wdi_popden wdi_popurb wdi_taxrev wdi_trade wdi_oilrent i.year i.pais,  cluster($cluster_level)
outreg2 using  "Output/vdem", replace ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit coupUnemp i.urban##c.taxBias $demogControls vdem_edcomp_thick wdi_gdpcapcur wdi_gdpgr wdi_gdpagr wdi_pop wdi_popden wdi_popurb wdi_taxrev wdi_trade wdi_oilrent i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/vdem", append ctitle(Coup (unemp.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit coupCorrupt i.urban##c.taxBias $demogControls vdem_edcomp_thick wdi_gdpcapcur wdi_gdpgr wdi_gdpagr wdi_pop wdi_popden wdi_popurb wdi_taxrev wdi_trade wdi_oilrent i.year i.pais,  cluster($cluster_level)
outreg2 using  "Output/vdem", append ctitle(Coup (corrupt.)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
probit coupCrime i.urban##c.taxBias $demogControls vdem_edcomp_thick wdi_gdpcapcur wdi_gdpgr wdi_gdpagr wdi_pop wdi_popden wdi_popurb wdi_taxrev wdi_trade wdi_oilrent i.year i.pais,  cluster($cluster_level)
outreg2 using  "Output/vdem", append ctitle(Coup (crime)) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))

* Table A12:  Placebo tests
//Spending on mil, %GDP
probit prefDem i.urban##c.wdi_expmil $demogControls $countryControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/placebos", replace ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
//Expenditure on education (% govt expenditure)
probit prefDem i.urban##c.wdi_expeduge $demogControls $countryControls  i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/placebos", append ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))
//Unemployment:
probit prefDem i.urban##c.wdi_unemp $demogControls $countryControls i.year i.pais, cluster($cluster_level)
outreg2 using  "Output/placebos", append ctitle(Pref. democ.) drop(i.pais pais* i.year year*) tex(frag) bdec(3) label adds(Countries, e(N_clust))

* Table A13:  Urban population shares
//NOTE:  This table was computed by hand based on WDI data on urban population share for the county in the year 2008 (or closest available year)
